package com.fighYo.trace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fighYo.trace.entity.Info;
import com.fighYo.trace.entity.Payment;
import com.fighYo.trace.entity.Product;
import com.fighYo.trace.entity.User;
import com.fighYo.trace.entity.vo.ProductVo;
import com.fighYo.trace.mapper.ProductMapper;
import com.fighYo.trace.service.InfoService;
import com.fighYo.trace.service.ProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fighYo.trace.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fighYo
 * @since 2022-04-04
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {

    @Autowired
    private UserService userService;

    @Autowired
    private InfoService infoService;

    @Override
    public boolean addQuantity(Payment payment) {

        String userId = payment.getBuyerId();
        User user = userService.getById(userId);

        Info userInfo = infoService.getUserInfo(userId);

        if (!user.getUserType().equals("3")) {
            return true;
        }

        String productId = payment.getProductId();

        QueryWrapper<Product> preWrapper = new QueryWrapper();
        preWrapper.eq("id", payment.getProductId());

        Product preProduct = baseMapper.selectOne(preWrapper);

        Integer speciesId = preProduct.getSpeciesId();
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("species_id", speciesId);
        wrapper.eq("user_id", userId);

        Product product = baseMapper.selectOne(wrapper);

        if (product != null) {
            product.setQuantity(product.getQuantity() + payment.getQuantity());
            baseMapper.updateById(product);
        } else {
            product = new Product();
            product.setUserId(String.valueOf(user.getId()));
            product.setProductName(preProduct.getProductName());
            product.setPrice(preProduct.getPrice());
            product.setSpeciesId(preProduct.getSpeciesId());
            product.setQuantity(payment.getQuantity());
            product.setArea(-1);
            product.setSaleFlag("0");
            product.setJudgeFlag("1");
            product.setAddress(userInfo.getAddress());
            product.setImg(preProduct.getImg());
            product.setUnit(preProduct.getUnit());
            product.setLon(preProduct.getLon());
            product.setLat(preProduct.getLat());
            baseMapper.insert(product);
        }


        return true;
    }

    @Override
    public ProductVo getProductVo(Product product) {
        String userId = product.getUserId();
        Info userInfo = infoService.getUserInfo(userId);
        ProductVo productVo = new ProductVo();
        BeanUtils.copyProperties(product, productVo);
        productVo.setUserName(userInfo.getName());
        return productVo;
    }
}
